package com.ihr360.cnb.module.insurance.service.impl;

import com.google.common.collect.Lists;
import com.ihr360.cnb.module.common.service.OrganizationService;
import com.ihr360.cnb.module.insurance.service.InsuranceAggregateService;
import com.ihr360.cnb.module.insurance.service.feign.InsuranceService;
import com.ihr360.cnb.module.oneclick.service.IrenshiDubboProviderService;
import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.commons.model.type.BenefitCategory;
import com.ihr360.insurance.entity.vo.BenefitListEntity;
import com.ihr360.insurance.entity.vo.CompanyBenefitSimpleVo;
import com.ihr360.insurance.entity.vo.IrenshiBenefitSimpleVo;
import com.ihr360.insurance.entity.vo.StaffBenefitEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Stone.Shi
 * @description
 * @date 2018-02-07 12:04:36.
 */
@Service
public class InsuranceAggregateServiceImpl implements InsuranceAggregateService {

    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private InsuranceService insuranceService;

    @Autowired
    private IrenshiDubboProviderService irenshiDubboProviderService;

    @Override
    public void saveStaffInsurance(String companyId, StaffBenefitEntity staffBenefitEntity) {
        //保存到福利云与irenshi两个模块
        insuranceService.saveStaffInsurance(companyId, staffBenefitEntity);
        irenshiDubboProviderService.saveStaffInsuranceInfo(companyId, staffBenefitEntity);
    }


    @Override
    public BenefitListEntity queryInsurance(String companyId) {
        BenefitListEntity entity = new BenefitListEntity();
        List<IrenshiBenefitSimpleVo> siBenefitSimpleVoList = Lists.newArrayList();
        List<IrenshiBenefitSimpleVo> hfBenefitSimpleVoList = Lists.newArrayList();

        List<CompanyBenefitSimpleVo> list = insuranceService.getCompanyBenefits(companyId);
        List<CompanyBenefitSimpleVo> siList = list.stream().filter(companyBenefitSimpleVo -> companyBenefitSimpleVo != null && BenefitCategory.SI.equals(companyBenefitSimpleVo.getCategory())).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(siList)) {
            siList.forEach(companyBenefitSimpleVo -> {
                IrenshiBenefitSimpleVo irenshiBenefitSimpleVo = new IrenshiBenefitSimpleVo();
                BeanUtils.copyProperties(companyBenefitSimpleVo, irenshiBenefitSimpleVo);
                irenshiBenefitSimpleVo.setId(companyBenefitSimpleVo.getId().toString());
                irenshiBenefitSimpleVo.setOld(false);
                String divisionId = companyBenefitSimpleVo.getDivisionId() == null ? "" : companyBenefitSimpleVo.getDivisionId().toString();
                irenshiBenefitSimpleVo.setDivisionId(divisionId);
                siBenefitSimpleVoList.add(irenshiBenefitSimpleVo);
            });
        }
        List<CompanyBenefitSimpleVo> hfList = list.stream().filter(companyBenefitSimpleVo -> companyBenefitSimpleVo != null && BenefitCategory.HF.equals(companyBenefitSimpleVo.getCategory())).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(hfList)) {
            hfList.forEach(companyBenefitSimpleVo -> {
                IrenshiBenefitSimpleVo irenshiBenefitSimpleVo = new IrenshiBenefitSimpleVo();
                BeanUtils.copyProperties(companyBenefitSimpleVo, irenshiBenefitSimpleVo);
                irenshiBenefitSimpleVo.setId(companyBenefitSimpleVo.getId().toString());
                irenshiBenefitSimpleVo.setOld(false);
                String divisionId = companyBenefitSimpleVo.getDivisionId() == null ? "" : companyBenefitSimpleVo.getDivisionId().toString();
                irenshiBenefitSimpleVo.setDivisionId(divisionId);
                hfBenefitSimpleVoList.add(irenshiBenefitSimpleVo);
            });
        }
        List<IrenshiBenefitSimpleVo> irenshiBenefitList = irenshiDubboProviderService.queryBenefitList(companyId);
        if (CollectionUtils.isNotEmpty(irenshiBenefitList)) {
            siBenefitSimpleVoList.addAll(irenshiBenefitList);
            List<IrenshiBenefitSimpleVo> hasFundList = irenshiBenefitList.stream().filter(IrenshiBenefitSimpleVo::getHasHouseFund).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(hasFundList)) {
                hfBenefitSimpleVoList.addAll(hasFundList);
            }
        }
        entity.setSiBenefitList(siBenefitSimpleVoList);
        entity.setHfBenefitList(hfBenefitSimpleVoList);
        return entity;
    }
}
